package br.edu.sistemaacademico;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import br.edu.sistemaacademico.Memento.Memento;
import br.edu.sistemaacademico.iterator.Iterator;
import br.edu.sistemaacademico.pojo.Aluno;
import br.edu.sistemaacademico.pojo.Historico;
import br.edu.sistemaacademico.state.State;
import br.edu.sistemaacademico.state.StateHandler;
import br.edu.sistemaacademico.util.Vetor;
import client.ClientCommand;


public class frmAlunoServidor extends FrmModelo{

	Aluno aluno;
	Iterator itAluno;
	
	StateHandler stateAlterarExcuir = new StateHandler(false);
    //List<Aluno> extra;
    Vetor vetorAluno;
	public List<Object> campos = ClientCommand.getCampos("AlunoService");//new ArrayList<Object>();
    private List<Object> valores = new ArrayList<Object>();
    private List<Object> tamanho = new ArrayList<Object>();
    TabelaModelo tabModel;
    
    public frmAlunoServidor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        tamanho.add("80");
        tabModel = new TabelaModelo(campos, tamanho, getJTable());
        tabModel.busca(getTfBuscar());
        ListSelectionModel listModel = getJTable().getSelectionModel();
        listModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        listModel.addListSelectionListener(list);
        
        setTitle("Aluno");
        
        
        setButtons();
        atualiza();
        setStateAlteraExcuir();
        setVisible(true);
    }

    public void setButtons(){
    	getbtnBusca().setActionCommand("buscar");
    	getbtnBusca().addActionListener(listener);
    	getJbtnGravar().setActionCommand("gravar");
        getJbtnGravar().addActionListener(listener);
        getbtnVoltar().addActionListener(listener);
        getbtnVoltar().setActionCommand("voltar");
        getbtnExcluir().setActionCommand("excluir");
        getbtnExcluir().addActionListener(listener);
        getbtnAlterar().setActionCommand("alterar");
        getbtnAlterar().addActionListener(listener);
        getbtn5().setActionCommand("matricula");
        getbtn5().addActionListener(listener);
        
        JButton btn1 = getJbtnGravar();
        JButton btn2 = getbtnAlterar();
        JButton btn3 = getbtnExcluir();
        JButton btn4 = getbtn4();
        JButton btn5 = getbtn5();
        JButton btn6 = getbtn6();
        btn1.setText("Novo");
        btn3.setText("Excluir");
        btn4.setText("Historico");
        btn5.setText("Matricula");
       // btn5.setVisible(false);
        btn6.setVisible(false);
    }
    public void atualiza(){
    	((DefaultTableModel) getJTable().getModel()).setNumRows(0);
		getJTable().updateUI();
		vetorAluno = new Vetor((List) ClientCommand.carregar("AlunoService", "codigo = codigo", ""));
    	itAluno = vetorAluno.createIterator();
    	
    	for (Object o =  itAluno.first(); !itAluno.isDone(); o =  itAluno.next()) {
    		Aluno a = (Aluno) o;
    		valores = new LinkedList<Object>();
    		valores.add(a.getId());
    		valores.add(a.getNome());
    		valores.add(a.getCpf());
    		valores.add(a.getEndereco());
    		valores.add(a.getTelefone());
    		valores.add(a.getSexo());
    		valores.add(a.getRg());
    		valores.add(a.getNascimento());
    		valores.add(a.getAnoIngresso());
    		valores.add(a.getCoeficiente());
    		valores.add(a.getLogin());
    		//valores.add(extra.get(i).getSenha());
    		valores.add("********");
    		valores.add("OK");
    		tabModel.setValores(valores);
    		valores.clear();
    	}
    }
    ActionListener listener = new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("gravar"))
				new frmCadastroAluno();
				//ClientCommand.gravar("AlunoService",aluno);
			if(e.getActionCommand().equals("voltar"))
				dispose();
			else if(e.getActionCommand().equals("turma")){
				FrmModelo frmT = new frmTurma(new Frame(), true);
			}else if(e.getActionCommand().equals("excluir"))
				remover();
			else if(e.getActionCommand().equals("alterar")){
				 new frmAtualizaAluno(aluno);
				 atualiza();
			}else if(e.getActionCommand().equals("matricula"))
				new frmFecharTurma(aluno);
			else if(e.getActionCommand().equals("buscar"))
				atualiza();
			atualiza();
		}
	};
    ListSelectionListener list = new ListSelectionListener() {
		
		@Override
		public void valueChanged(ListSelectionEvent e) {
			try{
				if(getJTable().getSelectedRow()>=0){
				aluno = getSelectedAluno(Long.parseLong(getJTable().getValueAt(getJTable().getSelectedRow(), 0).toString()));
				stateAlterarExcuir.change(true);}			
			}catch(ArrayIndexOutOfBoundsException ea){
				aluno = null;
				stateAlterarExcuir.change(false);
			}
		}
	};
	
    public void setStateAlteraExcuir(){
    	stateAlterarExcuir.addComponent(getbtnAlterar());
    	stateAlterarExcuir.addComponent(getbtnExcluir());
    	stateAlterarExcuir.addComponent(getbtn4());
    	stateAlterarExcuir.addComponent(getbtn5());
    }
	
	public Aluno getSelectedAluno(Long codigo){
		return (Aluno) ClientCommand.carregaObjeto("AlunoService","codigo="+codigo,"");
	}
	public void remover(){
		tabModel.delete(getJTable().getSelectedRow());
		ClientCommand.deletar("AlunoService",aluno);
		atualiza();
	}

}
